package com.kedll.library.util;

import com.kedll.library.listener.ICache;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by QiQi on 2017/8/22.
 */

public class LocalCache implements ICache {
    private static class SingleHolder {
        static LocalCache instance = new LocalCache();
    }

    public static LocalCache getInstance() {
        return SingleHolder.instance;
    }

    ConcurrentMap<String, Object> cacheMap = new ConcurrentHashMap<>();

    @Override
    public void put(String key, Object v) {
        cacheMap.put(key, v);
    }

    @Override
    public <E> E get(String key, E dft) {
        E v = (E) cacheMap.get(key);
        return v == null ? dft : v;
    }

    @Override
    public Object get(String key) {
        return cacheMap.get(key);
    }

    @Override
    public void remove(String key) {
        cacheMap.remove(key);
    }

    @Override
    public void clear() {
        cacheMap.clear();
    }
}
